//
//  SIStatusViewModel.swift
//  SinaImiate
//
//  Created by xiaoming on 16/7/5.
//  Copyright © 2016年 GoodJob. All rights reserved.
//

import UIKit

///单条微博的视图模型
class SIStatusViewModel: CustomStringConvertible {
    
    ///微博模型
    var status: SIStatus
    
    ///会员图标 - 
    var memberIcon: UIImage?
    
    ///认证图标
    var vipIcon: UIImage?
    ///转发文字
    var retweetedStr: String?
    ///评论文字
    var commentStr: String?
    ///点赞文字
    var likeStr: String?
    ///配图视图大小
    var pictureViewSize = CGSize()
    
    ///被转发微博的文字
//    var retransmissionText: String?
    
    /// 微博正文的属性文本
    var statusAttrText: AttributedString?
    /// 转发文字的微博文本
    var retweetedAttrText: AttributedString?
    
//    /// 来源字符串
//    var sourceStr: String?
    
    ///行高
    var rowHeight: CGFloat = 0
    
    ///原创或转发微博的urlString
    var picURLs: [SIStatusPicture]? {
        
        return status.retweeted_status?.pic_urls ?? status.pic_urls
        
    }
    
    ///根据当前的视图模型内容计算行高
    func updateRowHeight() {
        
        let margin: CGFloat = 12
        let iconHeight: CGFloat = 34
        let toolbarHeight: CGFloat = 35
        
        var height: CGFloat = 0
        
        let viewSize = CGSize(width: UIScreen.cz_screenWidth() - 2 * margin, height: CGFloat(MAXFLOAT))
        
        //顶部位置
        height = 2 * margin + iconHeight + margin
        
        if let text = statusAttrText {
            
            ///预期尺寸, 宽度固定, 高度尽量大
            ///选项, 换行文本, 统一使用 usesLineFragmentOrigin
            
            height += text.boundingRect(with: viewSize, options: [.usesLineFragmentOrigin], context: nil).height
        }
        
        ///判断是否是转发微博
        if status.retweeted_status != nil {
            
            height += 2 * margin
            
            if let text = retweetedAttrText {
                
                height += text.boundingRect(with: viewSize, options: [.usesLineFragmentOrigin], context: nil).height
            }
        }
        /// 配图视图
        height += pictureViewSize.height
        
        height += margin
        
        ///底部工具栏
        height += toolbarHeight
        
        ///使用属性记录
        rowHeight = height
        
    }
    
    ///构造函数
    init(model: SIStatus) {
        
        self.status = model
        
        if model.user?.mbrank > 0 && model.user?.mbrank < 7 {
            
            let imageName = "common_icon_membership_level\(model.user?.mbrank ?? 1)"
            
            memberIcon = UIImage(named: imageName)
            
        } else {
            
            memberIcon = #imageLiteral(resourceName: "common_icon_membership_expired")
        }
        
        switch model.user?.verified_type ?? -1 {
        case 0:
            vipIcon = #imageLiteral(resourceName: "avatar_vip")
        case 2, 3, 5:
            vipIcon = #imageLiteral(resourceName: "avatar_enterprise_vip")
        case 220:
            vipIcon = #imageLiteral(resourceName: "avatar_grassroot")
        default:
            break
        }
        ///设置底部数字字符串
        ///测试超过一万 
//        let demoCount = Int(arc4random_uniform(10000000))
        retweetedStr = countString(count: model.reposts_count, defaultStr: "转发")
        commentStr = countString(count: model.comments_count, defaultStr: "评论")
        likeStr = countString(count: model.attitudes_count, defaultStr: "赞")
        
        
        ///计算配图视图大小
        pictureViewSize = calcPictureViewSize(count: picURLs?.count)
        
        ///设置被转发的文字,
//        retransmissionText = "@" + (status.retweeted_status?.user?.screen_name ?? "") + ":" + (status.retweeted_status?.text ?? "")
        let orginalFont = UIFont.systemFont(ofSize: 14)
        let retweetedFont = UIFont.systemFont(ofSize: 13)
        
        statusAttrText = SIEmotionManager.shared.emoticonString(string: model.text ?? "", font: orginalFont)
        
        let rText = "@" + (status.retweeted_status?.user?.screen_name ?? "") + ":" + (status.retweeted_status?.text ?? "")
        
        retweetedAttrText = SIEmotionManager.shared.emoticonString(string: rText, font:retweetedFont)

        //计算行高
        updateRowHeight()
        
        
    }
    
    var description: String {
        
        return status.description
    }
    
    ///使用单个图像,更新配图视图的大小
    func updateSingleImageSize(image: UIImage) {
        
        
        var size = image.size
        
        print(size)
        
        let maxWidth: CGFloat = 200
        let minWidth: CGFloat = 40
        
        
        if size.width > maxWidth {
            
            //设置最大宽度
            size.width = maxWidth
            
            size.height = size.width * image.size.height / image.size.width
            
        }
        //FIXME: 图片过高,宽度正常处理
        if size.height > 200 {
            size.height = 200
        }
        
        if size.width < minWidth {
            
            size.width = minWidth
//
//            ///要特殊处理高度,否则高度太大,会影响用户体验
            size.height = size.width * image.size.height / image.size.width / 4
        }
        
        size.height += SIStatusPictureViewOutterMargin
        
        pictureViewSize = size
        
        updateRowHeight()
        
        
    }
    
    ///计算指定数量的图片对应的配图视图的大小
    private func calcPictureViewSize(count: Int?) -> CGSize {
        
        if count == 0 || count == nil {
            return CGSize()
        }
//        return CGSize(width: 100, height: 300)
       
        let row = ((count ?? 0) - 1) / 3 + 1
        
        var height = SIStatusPictureViewOutterMargin
        
        height += CGFloat(row) * SIStatusPictureItemWidth
        
        height += CGFloat((row ?? 0)-1) * SIStatusPictureViewInnerMargin
        
        
        return CGSize(width: SIStatusPictureViewWidth, height: height)
        
        
    }
    
    ///给定一个数字返回指定条件的字符串
    private func countString(count: Int, defaultStr: String) -> String {
        
        if count == 0 {
            
            return defaultStr
        }
        
        if count < 10000 {
            return count.description
        }
        
        return String(format: "%.02f 万", Double(count) / 10000)
        
    }
    
}
